﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Wiki.Entities;
using UMC.Web;

namespace UMC.Wiki.Activities
{
    class SubjectPortfolioSubActivity : WebActivity
    {
        public static List<WebMeta> Portfolio(Project team, ProjectItem project, int status)
        {
            var portfolios = DataFactory.Instance().Portfolio(project);

            var subs = new List<Subject>();
            if (portfolios.Length > 0)
            {
                subs.AddRange(DataFactory.Instance().Subject(portfolios));
            }
            var data = new List<WebMeta>();
            foreach (var p in portfolios.OrderBy(c => c.Sequence ?? 0))
            {
                var meta = new WebMeta();
                meta.Put("id", p.Id).Put("text", p.Caption);
                var csubs = subs.FindAll(s => s.portfolio_id == p.Id).OrderBy(c => c.Seq ?? 0);
                if (p.SubjectTotal != csubs.Count())
                {
                    DataFactory.Instance().Put(new Portfolio { Id = p.Id, SubjectTotal = csubs.Count() });
                    p.SubjectTotal = csubs.Count();
                }
                var dcsub = new List<WebMeta>();
                foreach (var cs in csubs)
                {
                    switch (status)
                    {
                        case -1:
                            if (cs.Status == 1)
                            {
                                var mta = new WebMeta().Put("id", cs.Id).Put("text", cs.Title);
                                mta.Put("path", String.Format("{0}/{1}/{2}", team.Code, project.Code, String.IsNullOrEmpty(cs.Code) ? "none" : cs.Code));
                                dcsub.Add(mta); ;
                            }
                            break;
                        default:
                            if (cs.Status > -2)
                            {
                                var mta = new WebMeta().Put("id", cs.Id).Put("text", cs.Title);
                                switch (cs.Status ?? 0)
                                {
                                    case 0:
                                        mta.Put("cls", "unrelease");
                                        break;
                                    case -1:
                                        mta.Put("cls", "warn");
                                        break;
                                }
                                mta.Put("path", String.Format("{0}/{1}/{2}", team.Code, project.Code, String.IsNullOrEmpty(cs.Code) ? "none" : cs.Code));
                                dcsub.Add(mta); ;
                            }
                            break;

                    }
                }
                meta.Put("subs", dcsub);

                if (dcsub.Count > 0)
                {
                    data.Add(meta);

                }
                else if (status > -1)
                {
                    data.Add(meta);
                }
            }
            return data;

        }
        void Portfolio(ulong projectItemid, WebResponse response)
        {
            var user = this.Context.Token.Identity();


            var projectItem = DataFactory.Instance().ProjectItem(projectItemid);

            var project = DataFactory.Instance().Project(projectItem.project_id.Value);
            int status = -1;
            var member = DataFactory.Instance().ProjectMember(project.Id.Value, user.Id.Value);
            if (member != null)
            {
                switch (member.AuthType)
                {
                    case WebAuthType.Admin:
                        status = 1;
                        break;
                    case WebAuthType.All:
                        status = -1;
                        break;
                    case WebAuthType.Guest:
                        status = -1;
                        break;
                    case WebAuthType.User:
                        status = 1;
                        break;
                }
            }

            var menu = new WebMeta();
            menu.Put("nav", String.Format("{0}/{1}", project.Code, projectItem.Code));
            menu.Put("subs", Portfolio(project, projectItem, status));
            response.Redirect(menu);

        }
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var Key = this.AsyncDialog("Key", "none");
            var pkd = UMC.Data.Utility.UInt64Parse(Key);
            if (pkd.HasValue)
            {
                Portfolio(pkd.Value, response);
            }
            else if (String.Equals("none", Key) == false)
            {
                var keys = Key.Split('/', StringSplitOptions.RemoveEmptyEntries);
                if (keys.Length > 1)
                {
                    var user = this.Context.Token.Identity();

                    var project = DataFactory.Instance().Project(keys[0]);
                    var projectItem = DataFactory.Instance().ProjectItem(project.Id.Value, keys[1]);

                    int status = -1;

                    var member = DataFactory.Instance().ProjectMember(project.Id.Value, user.Id.Value);
                    if (member != null)
                    {
                        switch (member.AuthType)
                        {
                            case WebAuthType.User:
                            case WebAuthType.Admin:
                                status = 1;
                                break;
                            default:
                            case WebAuthType.Guest:
                                status = -1;
                                break;
                        }
                    }



                    var menu = new WebMeta();
                    if (keys.Length == 3)
                    {
                        var sub = DataFactory.Instance().Subject(project.Id.Value, projectItem.Id.Value, keys[2]);

                        if (sub != null)
                        {
                            menu.Put("spa", new WebMeta().Put("id", sub.Id).Put("path", String.Format("{0}/{1}/{2}", project.Code, projectItem.Code, sub.Code)).Put("type", "subject"));
                        }
                    }
                    else
                    {
                        menu.Put("spa", new WebMeta().Put("id", projectItem.Id).Put("path", String.Format("{0}/{1}", project.Code, projectItem.Code)).Put("type", "item"));

                    }
                    menu.Put("nav", String.Format("{0}/{1}", project.Code, projectItem.Code));
                    menu.Put("subs", Portfolio(project, projectItem, status));
                    response.Redirect(menu);//

                }
            }




        }

    }
}